/*
 * @Descripttion: 
 * @version: 
 * @Author: lily
 * @Date: 2021-05-14 17:40:53
 * @LastEditors: lily
 * @LastEditTime: 2021-05-15 15:55:15
 */
/*
 * @lc app=leetcode.cn id=419 lang=javascript
 *
 * [419] 甲板上的战舰
 */

// @lc code=start
/**
 * @param {character[][]} board
 * @return {number}
 */

//  思想：
//  逐一扫描，如果发现当前是X，并且上边和右边不是X，则新增
//  边界由三目运算符进行处理

//  复杂度：O(n) n为board大小 O(1)

var countBattleships = function (board) {
    let res = 0
    for (let i = 0; i < board.length; i++) {
        for (let j = 0; j < board[0].length; j++) {
            let up = i > 0 ? board[i - 1][j] : ''
            let right = j > 0 ? board[i][j - 1] : ''
            if (board[i][j] === 'X') {
                if (up !== 'X' && right !== 'X') {
                    res++
                }
            }

        }
    }

    return res
};
// @lc code=end

console.log(countBattleships([["X"], ["X"], ["X"], ["X"]]));